﻿using B.S.BasicData.Api.Read.Application.Command;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using Common;
using MD5Hash;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace B.S.BasicData.Api.Read.Application.Handler
{
    public class GetLoginHandler : IRequestHandler<GetLoginCommand, ApiResult<LoginDTO>>
    {
        private readonly IBaseRepository<UserModel> userRope;
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IBaseRepository<UserRoleModel> userRoleRepo;
        private readonly IBaseRepository<DepartmentModel> departmentRepo;
        private readonly JwtTools jwtTools;
        private readonly IConfiguration configuration;

        public GetLoginHandler(IBaseRepository<UserModel> userRope, IBaseRepository<RoleModel> roleRepo,IBaseRepository<UserRoleModel> userRoleRepo,IBaseRepository<DepartmentModel> departmentRepo,JwtTools jwtTools,IConfiguration configuration)
        {
            this.userRope = userRope;
            this.roleRepo = roleRepo;
            this.userRoleRepo = userRoleRepo;
            this.departmentRepo = departmentRepo;
            this.jwtTools = jwtTools;
            this.configuration = configuration;
        }
        public Task<ApiResult<LoginDTO>> Handle(GetLoginCommand request, CancellationToken cancellationToken)
        {
            ApiResult<LoginDTO> res = new ApiResult<LoginDTO>();
            var login = userRope.GetAll().Where(x => x.UserName == request.Name).FirstOrDefault();
            if (login==null)
            {
                res.Msg = "账号不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var a = request.Pwd;
            if (login.Pwd!=a)
            {
                res.Msg = "密码错误";
                res.Code=ApiEnum.Error;
                return Task.FromResult(res);
            }
            var userrole = userRoleRepo.GetAll().Where(x => x.UserId == login.Id).Select(x=>x.RoleId).ToList();
            var rolename = new List<string>();
            foreach (var item in userrole)
            {
                var rolenames = roleRepo.GetAll().Where(x => x.Id == item).Select(x=>x.RoleName).FirstOrDefault();
                rolename.Add(rolenames);
            }
            var rid = new List<int>();
            foreach (var item in userrole)
            {
                var role = roleRepo.GetAll().Where(x => x.Id == item).Select(x => x.Id).FirstOrDefault();
                rid.Add(role);
            }
            var department = departmentRepo.GetAll().Where(x => x.Id == login.Bid).FirstOrDefault();
            var logindto = new LoginDTO
            {
                Bid = login.Bid,
                Bname = department.DepartmentName,
                Id = login.Id,
                RoleName = rolename,
                Rid = rid,
                UserName = login.UserName,
                JwtToken = jwtTools.JWTNewString(configuration, login.Id.ToString())
            };
            res.Msg = "登录成功";
            res.Code = ApiEnum.Success;
            res.Data= logindto;
            return Task.FromResult(res);
        }
        


    }
}
